
local ui = class.panel:builder 
{
    x = 1390,
    y = 400,
    w = 530,
    h = 400,
    show_flag = false,
    alpha = 0.99,
    fdf = 'tooltip_backdrop0',
}
ui:set_level(99)
ac.tools = ui

--图标
ui.icon = ui:add_panel('',12,10,64,64)
--标题
ui.title = ui:add_text('标题',12,10,ui.w,25,12)

--资源显示
ui.resources = ui:add_resources_panel(15,40,24,18)
ui.resources:add_type('cool',[[UI\CoolUI.blp]])
ui.resources:add_type('cost',[[UI\Widgets\ToolTips\Human\ToolTipManaIcon.blp]])
ui.resources:add_type('gold',[[UI\Widgets\ToolTips\Human\ToolTipGoldIcon.blp]])
ui.resources:add_type('lumber',[[UI\Widgets\ToolTips\Human\ToolTipLumberIcon.blp]])
ui.resources:add_type('food',[[UI\Feedback\Resources\ResourceSupply.blp]])


--介绍
ui.tip = ui:add_text('',12,50,ui.w-30,0,11)

-- 隐藏时 将原生提示框恢复
local id = japi.FrameGetTooltip()
--japi.FrameClearAllPoints(id)
function ui:hide()
    self.enter_button = nil
    class.panel.hide(self)
    japi.FrameSetPoint(japi.FrameGetTooltip(), 8, japi.GetGameUI(), 8, 0.0, 0.16)
end 


local tools = {
    tooltip = function (self, title, tip, anchor, width, resources,art)
        local ex_h = 0
        local width = width or 530
        --设置资源
        local open = ui.resources:set_info(resources)

        ui:set_width(width)
        ui.title:set_width(width)
        ui.tip:set_width(width-30)
        
        --内容
        ui.title:set_text(title)
        ui.tip:set_text(tip)

        japi.FrameSetPoint(japi.FrameGetTooltip(), 8, japi.GetGameUI(), 8, 0.3, 0.16)

        local x1,y1 = 12,10
        local x2,y2 = 15,40
        local x3,y3 = 12,50
        ui.icon:set_normal_image(art or '')
        if art then
            x1,y1 = 82,15
            x2,y2 = 85,45
            x3,y3 = 12,80
        else
            if open then
                y3 = y3 + 25
            end
        end
        ui.title:set_position(x1,y1)
        ui.resources:set_position(x2,y2)
        ui.tip:set_position(x3,y3)

        --总体高度
        local height = ui.tip.y + ui.tip:get_height() + 10
        ui:set_height(height)
        
        if anchor then 
            if anchor=='auto' then
                local x,y = self:get_real_position()
                if y>1080/2 then
                    self:set_tooltip_follow(ui, 'top')
                else
                    self:set_tooltip_follow(ui, 'bottom')
                end
            else
                self:set_tooltip_follow(ui, anchor)
            end
        else 
            local x, y = 1390, 785 - ui.h 
            ui:set_position(x, y)
        end 

        ui:show()

        ui.enter_button = self
        return ui
    end,

    skill_tooltip = function (self, skill, anchor)

        if self == nil then 
            self = ui 
        end
        if skill.temp_item then 
            self:item_tooltip(skill.temp_item, anchor)
            return 
        end 
        local title = skill:get_tooltips('title')
        local tip = skill:get_tooltips('tip')
        local resources = skill:get_tooltips('resource')
        local art = skill:get_art()
        if skill.shift_tip then
            local SHIFT = japi.GetKeyState(KEY.SHIFT)
            if SHIFT then
                tip = skill:get_shift_tip()
            end
            tip = tip .. [[|n|n按|cffff0000shift|r查看详细数据]]
        end
        local tool = self:tooltip(title, tip, anchor, nil, resources,art)

        return tool
    end,

    item_tooltip = function (self, item, anchor)
        if self == nil then 
            self = ui 
        end
        local title = item:get_tooltips('title')
        local tip = item:get_tooltips('tip')
        local resources = item:get_tooltips('resource')
        local art = item:get_art()
       
        return self:tooltip(title, tip, anchor,nil,resources,art)
    end,
    

    remove_tooltip = function ()
        ui.enter_button = nil
        ui:hide()
    end,
}


local real_remove = ac.frame.remove_tooltip
for name, func in pairs(tools) do 
    ac.frame[name] = func
end 
tools.old_remove_tooltip = real_remove




local message = require 'jass.message'
ac.console:event '控制台-指向技能按钮'(function(_,button,unit)
    local unit = unit or ac.unit(japi.GetRealSelectUnit())
    local ability_id, order_id = message.button(button.column,button.row)
    ac.tools.button = button
    if unit and unit.play_skills and ability_id then 
        local skill = unit.play_skills[ability_id]
        if skill then
            button:skill_tooltip(skill)
        else
            button:remove_tooltip()
        end
    end
end)

ac.console:event '控制台-离开技能按钮'(function(_,button,unit)
    ac.tools.button = nil
    button:remove_tooltip()
end)

ac.console:event '控制台-指向物品按钮'(function(_,button,unit)
    local unit = unit or ac.unit(japi.GetRealSelectUnit())
    ac.tools.button = button
    if unit then 
        local item = unit.items and unit.items[button.slotid]
        if item then
            button:item_tooltip(item)
            ac.console:notify('控制台-高亮物品按钮',button)
        else
            button:remove_tooltip()
        end
    end
end)

ac.console:event '控制台-离开物品按钮'(function(_,button,unit)
    ac.tools.button = nil
    button:remove_tooltip()
end)


ac.console:event "控制台-刷新技能显示" (function(_, unit)
    local unit = unit or ac.unit(japi.GetRealSelectUnit())
    local button = ac.tools.button
    if unit and button and button.command == "ability" then 
        ac.console:notify("控制台-离开技能按钮", button, unit)
        ac.console:notify("控制台-指向技能按钮", button, unit)
    end
end) 

ac.console:event "控制台-刷新物品显示" (function(_, unit)
    local unit = unit or ac.unit(japi.GetRealSelectUnit())
    local button = ac.tools.button
    if unit and button and button.command == "item" then 
        ac.console:notify("控制台-离开物品按钮", button, unit)
        ac.console:notify("控制台-指向物品按钮", button, unit)
    end
end)

ac.game:event '玩家-刷新UI'(function(_,player)
    if player:is_self() then
        local button = ui.enter_button
        if button then
            button:remove_tooltip()
            button:notify('on_button_mouse_enter')
        end
    end
end)

ac.game:event '本地消息-选择单位'(function(_,unit)
    ac.console:notify("控制台-刷新技能显示", unit)
    ac.console:notify("控制台-刷新物品显示", unit)
end)


